// 使用deepseek编写
class SubliminalProcessor extends AudioWorkletProcessor {
    static get parameterDescriptors() {
      return [{
        name: 'action',
        defaultValue: 0,
        minValue: 0,
        maxValue: 1
      }];
    }
  
    constructor() {
      super();
      // 常量定义
      this.carrierFreq = 16000;  // 载波频率
      this.lowFreq = 300;        // 低通频率
      this.highFreq = 3000;      // 高通频率
      this.encGain = 5;          // 编码增益
      this.decGain = 4;          // 解码增益
      
      // 动态压缩参数
      this.limitThreshold = 0.8;
      this.peakEnv = 1;
      this.sampleCounter = 0;
    }
  
    process(inputs, outputs, parameters) {
      const input = inputs[0];
      const output = outputs[0];
      const action = parameters.action[0] || 0;
  
      for (let channel = 0; channel < input.length; channel++) {
        const inputChannel = input[channel];
        const outputChannel = output[channel];
        
        // 编码处理流程
        if (action === 0) {
          let filtered = this.audioFilter(inputChannel);
          let modulated = this.modulate(filtered);
          let encoded = this.sidebandFilter(modulated);
          encoded = this.limit(encoded, this.encGain);
          for (let i = 0; i < outputChannel.length; i++) {
            outputChannel[i] = encoded[i];
          }
        }
        // 解码处理流程
        else {
          let decoded = this.demodulate(inputChannel);
          decoded = this.limit(decoded, this.decGain);
          for (let i = 0; i < outputChannel.length; i++) {
            outputChannel[i] = decoded[i];
          }
        }
      }
      return true;
    }
  
    // 音频带通滤波（模拟8阶滤波器）
    audioFilter(signal) {
      const filtered = new Float32Array(signal.length);
      // 简化的二阶滤波器模拟高阶效果
      let y1 = 0, y2 = 0, x1 = 0, x2 = 0;
      const cutoff = this.lowFreq / sampleRate;
      for (let i = 0; i < signal.length; i++) {
        const y = signal[i] - 2 * x1 + x2 + 0.3 * y1 - 0.1 * y2;
        filtered[i] = y;
        x2 = x1;
        x1 = signal[i];
        y2 = y1;
        y1 = y;
      }
      return filtered;
    }
  
    // 调制处理（AM调制）
    modulate(signal) {
      const modulated = new Float32Array(signal.length);
      const phaseStep = (2 * Math.PI * this.carrierFreq) / sampleRate;
      let phase = 0;
      for (let i = 0; i < signal.length; i++) {
        modulated[i] = signal[i] * Math.cos(phase);
        phase += phaseStep;
        if (phase > 2 * Math.PI) phase -= 2 * Math.PI;
      }
      return modulated;
    }
  
    // 边带滤波（模拟多次高通）
    sidebandFilter(signal) {
      const cutoff = (this.carrierFreq + this.lowFreq) / sampleRate;
      let filtered = signal;
      // 简化的多阶高通滤波
      for (let n = 0; n < 8; n++) {
        const temp = new Float32Array(filtered.length);
        let y1 = 0, x1 = 0;
        for (let i = 0; i < filtered.length; i++) {
          temp[i] = filtered[i] - x1 + 0.995 * y1;
          x1 = filtered[i];
          y1 = temp[i];
        }
        filtered = temp;
      }
      return filtered;
    }
  
    // 解调处理
    demodulate(signal) {
      const decoded = new Float32Array(signal.length);
      const phaseStep = (2 * Math.PI * this.carrierFreq) / sampleRate;
      let phase = 0;
      for (let i = 0; i < signal.length; i++) {
        decoded[i] = signal[i] * Math.cos(phase);
        phase += phaseStep;
        if (phase > 2 * Math.PI) phase -= 2 * Math.PI;
      }
      // 低通滤波
      let y1 = 0;
      const cutoff = this.highFreq / sampleRate;
      for (let i = 0; i < decoded.length; i++) {
        const y = decoded[i] + (1 - cutoff) * y1;
        decoded[i] = y;
        y1 = y;
      }
      return decoded;
    }
  
    // 动态限幅器
    limit(signal, gain) {
      const limited = new Float32Array(signal.length);
      let peak = 0;
      // 检测峰值
      for (let i = 0; i < signal.length; i++) {
        peak = Math.max(peak, Math.abs(signal[i]));
      }
      // 动态增益控制
      const target = this.limitThreshold / Math.max(peak, 1e-5);
      this.peakEnv = 0.99 * this.peakEnv + 0.01 * target;
      
      // 应用增益
      for (let i = 0; i < signal.length; i++) {
        limited[i] = signal[i] * gain * this.peakEnv;
      }
      return limited;
    }
  }
  
  registerProcessor('subliminal-processor', SubliminalProcessor);